package utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import com.aton.db.Config;

import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

/**
 * 调用shell脚本工具类
 * @author wangshaoming
 *
 */
public class ShellUtil {
	private static Connection conn;
//	private static String ipAddr=Config.getProperty("ssh.ip");
//	private static String userName=Config.getProperty("ssh.user");
//	private static String password=Config.getProperty("ssh.pass");
	private static String charset=Charset.defaultCharset().toString();
	private static int TIME_OUT=1000*5*60;
	
	/**
	 * 登录
	 * @return
	 */
//	public static boolean login(){
//		conn=new Connection(ipAddr);
//		try {
//			conn.connect();
//			return conn.authenticateWithPassword(userName, password);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			return false;
//		}
//	}
	/**
	 * 登录
	 * @return
	 */
	public static boolean login(String ipAddr,String user,String pass){
		conn=new Connection(ipAddr);
		try {
			conn.connect();
			return conn.authenticateWithPassword(user, pass);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 执行脚本
	 * @param cmds 命令
	 * @param param 参数
	 * @return
	 */
//	public static Map<String,Object> exec(String cmds){
//		InputStream stdOut=null;
//		InputStream stdErr=null;
//		String outStr="";
//		String outErr="";
//		Map<String,Object> map=new HashMap<String,Object>();
//		int res=-1;
//		try{
//			if(login()){
//				Session session=conn.openSession();
//				session.execCommand(cmds);
//				stdOut=new StreamGobbler(session.getStdout());
//				outStr=processStram(stdOut,charset);
//				stdErr=new StreamGobbler(session.getStderr());
//				outErr=processStram(stdErr,charset);
//				session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
//				res=session.getExitStatus();
//				map.put("outPut", outStr);
//				map.put("outErr", outErr);
//			}
//		}catch(Exception e){
//			e.printStackTrace();
//		}finally{
//			if(conn!=null){
//				conn.close();
//			}
//		}
//		map.put("status", res);
//		return map;
//	}
	/**
	 * 执行脚本
	 * @param cmds 命令
	 * @param param 参数
	 * @return
	 */
	public static Map<String,Object> execAndPass(String addr,String user,String pass,String cmds){
		InputStream stdOut=null;
		InputStream stdErr=null;
		String outStr="";
		String outErr="";
		Map<String,Object> map=new HashMap<String,Object>();
		int res=-1;
		try{
			if(login(addr,user,pass)){
				Session session=conn.openSession();
				session.execCommand(cmds);
				stdOut=new StreamGobbler(session.getStdout());
				outStr=processStram(stdOut,charset);
				stdErr=new StreamGobbler(session.getStderr());
				outErr=processStram(stdErr,charset);
				session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
				res=session.getExitStatus();
				map.put("outPut", outStr);
				map.put("outErr", outErr);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null){
				conn.close();
			}
		}
		map.put("status", res);
		return map;
	}
	/**
	 * 输出
	 * @param in
	 * @param charset
	 * @return
	 */
	public static String processStram(InputStream in, String charset){
		byte[] buf=new byte[1024];
		StringBuffer sb=new StringBuffer();
		try {
			while(in.read(buf)!=-1){
				sb.append(new String(buf,charset));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	}
}
